package com.example.activity_usage.datatrans;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.activity_usage.R;
import com.example.activity_usage.utils.DateUtils;

public class ReceiveDataActivity extends AppCompatActivity {

    private TextView textReceive;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_receive_data);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        textReceive = findViewById(R.id.text_receive);

        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            String requestTime = bundle.getString("requestTime");
            String requestContent = bundle.getString("requestContent");
            textReceive.setText(String.format("收到请求数据。\n请求时间：%s\n请求内容：%s", requestTime, requestContent));
        }

        // 设置响应数据
        Intent respIntent = new Intent();
        Bundle respBundle = new Bundle();
        respBundle.putString("respTime", DateUtils.getFormattedTimeNow());
        respBundle.putString("respContent", "天气是还不错，下了一点小雨，要是不下雨就更好了。");
        respIntent.putExtras(respBundle);
        setResult(RESULT_OK, respIntent);
    }
}